package com.shopFund;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import shopBean.common.Exception.MyException;
import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.RestResultGenerator;
import shopBean.common.utils.UUIDGenerator;

@ControllerAdvice
public class RestExceptionHandler {
	private static final Logger log = LoggerFactory.getLogger(RestExceptionHandler.class);

    @ExceptionHandler
    @ResponseBody
    private <T> RestResult<T> myExceptionExceptionHandler(Exception e) {
    		String uuid = UUIDGenerator.generate38();
    		log.error("errorid:"+uuid);
    		log.error("---------> huge error!", e);
    		if(e instanceof MyException) {
    			return RestResultGenerator.genErrorResult(e.getMessage(),uuid,((MyException)e).getErrorCode());
    		}
    		if(StringUtils.isBlank(e.getMessage())) {
    			 return RestResultGenerator.genErrorResult("系统异常",uuid,Status.sys_error);
    		}
        return RestResultGenerator.genErrorResult(e.getMessage(),uuid,Status.sys_error);
    }
   

}
